
from errors import error

class NotLoopQueue(object):

    def __init__(self, max_size):
        self.max_size = max_size
        self.front = self.rear = -1
        self.arr = [0] * max_size

    def push(self, value):
        if self.rear == self.max_size-1:
            return error.FError("队列满~")
        self.rear = self.rear + 1
        self.arr[self.rear] = value
        return None

    def pop(self):
        if self.front == self.rear:
            return error.FError("队列空"), None
        self.front = self.front + 1
        return None, self.arr[self.front]

    def list(self):
        if self.front == self.rear:
            print("队列空")
            return
        for i in range(self.front+1, self.rear+1):
            print("arr[{}]={}".format(i, self.arr[i]))


if __name__ == '__main__':
    queue = NotLoopQueue(5)
    flag = True
    while flag:
        print("输入push入队")
        print("输入pop出队")
        print("输入list循环")
        print("输入exit退出")
        key = input()
        if key == "push":
            value = input("请输入：")
            err = queue.push(value)
            if err:
                print(err.message)
            else:
                print("添加成功")
        elif key == "pop":
            err, value = queue.pop()
            if err:
                print(err.message)
            else:
                print(value)
        elif key == "list":
            queue.list()
        elif key == "exit":
            flag = False
        else:
            print("请输入正确的指令")
